#include "RankingSelection.h"

Solution RankingSelection::Select( std::vector<Solution> &output )
{
	unsigned int ranking[100] = {0} ,random[100] = {0} ,result = 0 
		,x = (1 + output.size()) * output.size() / 2 ,y = 5 ,r = rand() % (x + output.size() * y);
	for (unsigned int i = 0; i < output.size(); i++){
		for (unsigned int j = 0; j < output.size(); j++){
			if(i < j){
				if(output[i].getPreferEvaluate() > output[j].getPreferEvaluate()){ranking[i]++;}
			}
			else if(i > j){
				if(output[i].getPreferEvaluate() >= output[j].getPreferEvaluate()){ranking[i]++;}
			}
		}
		if(i > 0){random[i] = random[i - 1] + output.size() - i + y;}
		else{random[i] = output.size() + y;}
	}
	for (result = 0; result < output.size(); result++){
		if(r < random[result]){break;}
	}
	return output[result];
}